All files utility.js

1.28% Statements 1/78
0% Branches 0/54
0% Functions 0/9
1.29% Lines 1/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170      1x                                                                                                                                                                                                                                                                                                                                            
import {getUrlParameter} from "./fetch.stream.js";
import "./external/loglevel.min.js";
 
const Logger = window.log;
/**
 *
 * @param {number} n
 * @returns
 */
export function generateRandomString(n) {
    let randomString = '';
    let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
    for (let i = 0; i < n; i++) {
        randomString += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return randomString;
}
/**
 *
 * @param {Location} location
 * @param {string} rtmpForward
 * @returns
 */
export function getWebSocketURL(location, rtmpForward) {
    var appName = location.pathname.substring(1, location.pathname.indexOf("/", 1) + 1);
    var path = location.hostname + ":" + location.port + "/" + appName + "websocket";
    if (typeof rtmpForward != "undefined") {
        path += "?rtmpForward=" + rtmpForward;
    }
    var websocketURL = "ws://" + path;
 
    if (location.protocol.startsWith("https")) {
        websocketURL = "wss://" + path;
    }
    return websocketURL;
}
/**
 *
 * @param {Location} location
 * @param {string} id
 * @param {number} port
 * @returns
 */
export function getSrtURL(location, id, port) {
    var appName = location.pathname.substring(1, location.pathname.indexOf("/", 1) + 1);
    return "srt://" + location.hostname + ":" + port + "?streamid=" + appName + id;
 
}
/**
 *
 * @param {Location} location
 * @param {string} id
 * @returns
 */
export function getRtmpUrl(location, id) {
    var appName = location.pathname.substring(1, location.pathname.indexOf("/", 1) + 1);
    return "rtmp://" + location.hostname + "/" + appName + id;
}
/**
 *
 * @param {string} paramName
 * @returns
 */
export function getQueryParameter(paramName) {
    var value = getUrlParameter(paramName);
    if (typeof value != "undefined") {
        return "&" + paramName + "=" + value;
    }
    return "";
    //if it does not match, it returns "undefined"
 
}
/**
 *
 * @param {string} streamId
 * @param {string} linkUrl
 */
export function updateBroadcastStatusInfo(streamId, linkUrl) {
    $("#offlineInfo").hide();
    $("#broadcastingInfo").show();
    if (linkUrl === undefined) {
        linkUrl = "../play.html?id=" + streamId;
    }
    $("#playlink").attr("href", linkUrl)
    $("#playlink").show();
 
    setTimeout(function () {
        var state = window.webRTCAdaptor.signallingState(streamId);
        if (state != null && state != "closed") {
            var iceState = window.webRTCAdaptor.iceConnectionState(streamId);
            if (iceState != null && iceState != "failed" && iceState != "disconnected") {
                updateBroadcastStatusInfo(streamId, linkUrl);
            } else {
                $("#playlink").hide();
                $("#broadcastingInfo").hide();
                $("#offlineInfo").show();
            }
        } else {
            $("#playlink").hide();
            $("#broadcastingInfo").hide();
            $("#offlineInfo").show();
        }
    }, 200);
 
}
 
/**
 * @returns {boolean}
 */
export function isAndroid() {
    let userAgent = navigator.userAgent || navigator.vendor || window.opera;
    return /android/i.test(userAgent);
}
 
/**
 *
 * @param {*} error
 * @param {string} message
 * @returns
 */
export function errorHandler(error, message) {
    Logger.warn("error callback: " + JSON.stringify(error));
    var errorMessage = JSON.stringify(error);
    if (typeof message != "undefined") {
        errorMessage = message;
    } else {
        errorMessage = JSON.stringify(error);
    }
 
    if (error.indexOf("WebSocketNotConnected") != -1) {
        errorMessage = "WebSocket is disconnected.";
    } else if (error.indexOf("not_initialized_yet") != -1) {
        errorMessage = "Server is getting initialized.";
    } else if (error.indexOf("data_store_not_available") != -1) {
        errorMessage = "Data store is not available. It's likely that server is initialized or getting closed";
    } else {
        if (error.indexOf("NotFoundError") != -1) {
            errorMessage = "Camera or Mic are not found or not allowed in your device";
        } else if (error.indexOf("NotReadableError") != -1 || error.indexOf("TrackStartError") != -1) {
            errorMessage = "Camera or Mic are already in use and they cannot be opened. Choose another video/audio source if you have on the page below ";
 
        } else if (error.indexOf("OverconstrainedError") != -1 || error.indexOf("ConstraintNotSatisfiedError") != -1) {
            errorMessage = "There is no device found that fits your video and audio constraints. You may change video and audio constraints"
        } else if (error.indexOf("NotAllowedError") != -1 || error.indexOf("PermissionDeniedError") != -1) {
            errorMessage = "You are not allowed to access camera and mic.";
        } else if (error.indexOf("TypeError") != -1) {
            errorMessage = "Video/Audio is required";
        } else if (error.indexOf("getUserMediaIsNotAllowed") != -1) {
            errorMessage = "You are not allowed to reach devices from an insecure origin, please enable ssl";
        } else if (error.indexOf("ScreenSharePermissionDenied") != -1) {
            errorMessage = "You are not allowed to access screen share";
            $(".video-source").first().prop("checked", true);
        } else if (error.indexOf("UnsecureContext") != -1) {
            errorMessage = "Please Install SSL(https). Camera and mic cannot be opened because of unsecure context. ";
        }
        else if (error.indexOf('no_stream_exist') != -1) {
            errorMessage = 'There is no active live stream with this id to play';
        } else {
            errorMessage = error
        }
    }
    Logger.error(errorMessage);
    if (message !== undefined) {
        Logger.error(message);
    }
    return errorMessage;
}